package com.event.usc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.event.usc.dto.User;
import com.event.usc.dto.UserEvent;
import com.event.usc.oracleJDBC.DatabaseHandler;
import com.google.gson.Gson;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String qtype = request.getParameter("q");
		if (qtype.equals("signup"))
		{
			String userid = request.getParameter("userid");
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String password = request.getParameter("password");
			doSignupRequest(userid, firstname, lastname, password, response);
		}
		else if (qtype.equals("login"))
		{
			String userid = request.getParameter("userid");
			String password = request.getParameter("password");
			doLoginRequest(userid, password, response);
		}
		else if (qtype.equals("addfd"))
		{
			String userid = request.getParameter("userid");
			String friendid = request.getParameter("friendid");
			doAddFDRequest(userid, friendid, response);
		}
		else if (qtype.equals("showfd"))
		{
			String userid = request.getParameter("userid");
			doShowFDRequest(userid, response);
		}
	}

	private void doShowFDRequest(String userid, HttpServletResponse response) {
		
		try {
			DatabaseHandler handler = new DatabaseHandler();  
			printResponse(response, handler.getAllFd(userid));
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}	
	}

	private void printResponse(HttpServletResponse response,
			List<UserEvent> allFd) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        if (allFd==null||allFd.size()==0)
        	out.write("[]");
        else
        {
        	Gson gson = new Gson();
        	out.write(gson.toJson(allFd));
        }
	}

	private void doAddFDRequest(String userid, String friendid,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();  
			if (handler.addFriend(userid, friendid))
				printResponse(response, "{ 'status' : 'success' }");
			else
				printResponse(response, "{ 'status' : 'error' }");
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}	
		
	}

	private void doLoginRequest(String userid, String password,
			HttpServletResponse response) throws IOException {
		int status = checkUserExist(userid);
		if (status == 0)
			printResponse(response, "{'status':'not exist'}");
		else if (status < 0)
			printResponse(response, "{'status':'error'}");
		else
			userLogin(userid, password, response);
	}

	private void userLogin(String userid, String password,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();         
			printResponse(response, userid, password, handler.loginUser(userid, password));
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}	
	}

	private void printResponse(HttpServletResponse response, String userid, String password, User loginUser) throws IOException {
		
		if (loginUser.getUserId()==null||!loginUser.getUserId().equals(userid))
			printResponse(response, "{'status':'error'}");
		else if (!loginUser.getUserPassword().equals(password))
			printResponse(response, "{'status':'wrong password'}");
		else
			printResponse(response, "{'status':'success'}");	
	}
	private void doSignupRequest(String userid, String firstname, String lastname, String password,
			HttpServletResponse response) throws IOException {
		int status = checkUserExist(userid);
		if (status==0)
			userSignup(userid, firstname, lastname, password, response);
		else if (status>0)
			printResponse(response, "{'status':'exist'}");
		else
			printResponse(response, "{'status':'error'}");
	}

	private void userSignup(String userid, String firstname, String lastname, String password,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();         
			if (handler.insertUser(userid, firstname, lastname, password))
				printResponse(response, "{'status':'success'}");
			else
				printResponse(response, "{'status':'error'}");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}	
	}

	private void printResponse(HttpServletResponse response, String string) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write(string);	
	}

	private int checkUserExist(String userid) {
		try {
			DatabaseHandler handler = new DatabaseHandler();         
			int result = handler.checkUser(userid);
			handler.closeConnection();
			return result;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
